package com.room.room.controller;

import java.util.Arrays;
import java.util.Map;

import com.project.utils.PageUtils;
import com.project.utils.R;

import com.room.room.entity.HouseCollectEntity;
import com.room.room.service.HouseCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;





/**
 * 
 *
 * @author brh
 * @email sunlightcs@gmail.com
 * @date 2022-04-01 14:56:53
 */
@RestController
@RequestMapping("room/housecollect")
public class HouseCollectController {
    @Autowired
    private HouseCollectService houseCollectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = houseCollectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{collectId}")
    public R info(@PathVariable("collectId") Integer collectId){
		HouseCollectEntity houseCollect = houseCollectService.getById(collectId);

        return R.ok().put("houseCollect", houseCollect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HouseCollectEntity houseCollect){
        //判断用户是否已经收藏过此房屋
        HouseCollectEntity house = houseCollectService.findByHouse(houseCollect.getSecondaryName());

        if(house!=null && house.getSecondaryName().equals(houseCollect.getSecondaryName())){
            return R.error("不能重复收藏");
        }else if (house==null){//没有收藏过
            houseCollectService.save(houseCollect);
            return R.ok();
        }
        return null;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HouseCollectEntity houseCollect){
		houseCollectService.updateById(houseCollect);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] collectIds){
		houseCollectService.removeByIds(Arrays.asList(collectIds));

        return R.ok();
    }

}
